<?php
/**
*登录控制器
*/
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController
{   
    public function index()
    {   
        $sql = "select * from vercode order by rand() limit 1";
        $res = M()->query($sql);
        $_SESSION['answer']=$res[0]['answer'];
        $this->assign('res',$res);
        $this->display();
    }

   
    public function checkecode()
    {
        $vercode =I('post.vercode','');

        
        if($vercode!=$_SESSION['answer']){
            $arr = array(
                'error'=>1,
                'info'=>'人类验证不通过'
                );
            echo json_encode($arr);
        }else{
            $arr = array(
                'error'=>0,
                'info'=>'通过'
                );
            echo json_encode($arr);
        }
    }

  
    public function getcode()
    {
        $sql = "select * from vercode order by rand() limit 1";
        $res = M()->query($sql);
        $_SESSION['answer']=$res[0]['answer'];
        echo json_encode(array("info"=>$res[0]['code']));
    }

      public function checke()
    {  

        $data =I('post.data');
        $email =$data['email'];

        $password =md5($data['repass']);
        $sql = "select * from user where email='{$email}'";
        $res = M()->query($sql);
        if (!$res) {
            $arr = array('error' =>0 ,'info'=>'邮箱不存在' );
            echo json_encode($arr);
        }else if($password!=$res[0]['password']){
            $arr = array('error' =>0 ,'info'=>'密码不正确' );
            echo json_encode($arr);
        }else{
           
            $_SESSION['uid'] = $res[0]['id'];
            $_SESSION['nikename'] = $res[0]['nikename'];
            $_SESSION['face'] = $res[0]['face'];
            echo json_encode(array('error'=>1,'info'=>'登录成功'));
        }
       
      
    }
    public function out()
    {
        session_unset();
        session_destroy();
        $this->success('退出成功',U('home/index/index'));
    }


    public function QQreturn()
    {   


       $openid = getOpenid();
       $sql = "select * from user where openid = '{$openid}'";
       $res = M()->query($sql);
       if($res){
            $_SESSION['nikename'] = $res[0]['nikename'];
            $_SESSION['uid'] = $res[0]['id'];
            $_SESSION['face'] = $res[0]['face']; 
            $this->success('登录成功',U('home/usercore/index'));
       }else{
            $userinfo = getinfo();
            $nike = $userinfo['nickname'];
            $sql = "select * from user where nikename = '{$nike}'";
            $res = M()->query($sql);var_dump($res);
            $userinfo['sex']=$userinfo['gender'];
            $userinfo['face']=getimg($userinfo['figureurl_qq_2']);
            $userinfo['create_time']=time();
            $userinfo['openid']=$Openid;
            $userinfo['nikename']=$userinfo['nickname'];
            $userinfo['openid']=$openid;
  
                   
                    var_dump($userinfo['nikename']);
                    $res = M('user')->add($userinfo);

            if ($res) {
                $_SESSION['nikename'] = $userinfo['nikename'];
                $_SESSION['uid'] = $res;
                $_SESSION['face'] = $userinfo['face'];
                $this->success('登录成功',U('home/usercore/index'));
            }else{
                $this->error('qq绑定失败，请重新绑定',U('home/login/index'));
            }

       }




       

    }

    public function qqLogin()
    {
        qqloginfn();
    }
 
}
